<?php

/**
 * Generate automatically class definitions related to database table definition.
 * 
 * @author keiji
 * @static 
 */
class MappedClassGenerator {

	private static $_TEMPORARY_CLASS_NAME;
	private static $_TEMPORARY_PROPERTY_NAMES;
	
	public static function generateClassDefinitions($saveFolderPath) {
		$connection = DatabaseManager::getDatabaseConnection();
		$tablesResult = mysql_query('show tables;', $connection);
		while ($tableInformation = mysql_fetch_array($tablesResult)) {
			MappedClassGenerator::$_TEMPORARY_CLASS_NAME = $tableInformation[0];
			MappedClassGenerator::$_TEMPORARY_PROPERTY_NAMES = array();
			$describeResult = mysql_query('describe ' . MappedClassGenerator::$_TEMPORARY_CLASS_NAME . ";", $connection);
			while ($columnInfomation = mysql_fetch_array($describeResult)) {
				MappedClassGenerator::$_TEMPORARY_PROPERTY_NAMES[] = $columnInfomation['Field'];
			}
			MappedClassGenerator::_saveGeneratedClassDescription();
		}
	}
	
	private static function _getSaveFolderPath() {
		return dirname(__FILE__) . DIRECTORY_SEPARATOR . 'generatedClasses' . DIRECTORY_SEPARATOR;
	}
	private static function _getTemplatePath() {
		return MappedClassGenerator::_getSaveFolderPath() . 'generated_class_template.php';
	}
	private static function _saveGeneratedClassDescription() {
		ob_start();
		$className = MappedClassGenerator::$_TEMPORARY_CLASS_NAME;
		$propertyNames = MappedClassGenerator::$_TEMPORARY_PROPERTY_NAMES;
		require MappedClassGenerator::_getTemplatePath();
		$classDefinitionSource = ob_get_clean();
		$savedClassDefinitionFilePath = MappedClassGenerator::_getSaveFolderPath() . MappedClassGenerator::$_TEMPORARY_CLASS_NAME . '.class.php';
		file_put_contents($savedClassDefinitionFilePath, $classDefinitionSource);
	}
}

//require_once dirname(__FILE__) . '/../autoloadplus/bin/include.php';
//ALP_ClassDefLocMap::initialize(dirname(__FILE__) . '/inc_mapping.txt');

//MappedClassGenerator::generateClassDefinitions(null);
?>